<template>
    <div class="mod-config">
        <!--  头部  -->
        <div>
            <!--  注册用户 -->
            <el-button type="primary" icon="el-icon-circle-plus-outline" @click="registerUser()"
                       v-if="permissions.registerUser">注册员工
            </el-button>
            <!--  搜索内容  -->
            <el-form :inline="true" style="float:right">
                <el-form-item>
                    <el-button icon="el-icon-search" type="primary" circle @click="search"></el-button>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="searchName" placeholder="搜索姓名例如：张三" size="medium" class="input"
                              clearable="clearable"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <el-divider v-if="permissions.registerUser"></el-divider>
        <!--  主体表格  -->
        <div class="table">
            <el-table :data="tableData" border style="width: 100%" v-loading="dataListLoading">
                <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
                    <template #default="scope">
                        <span>{{ (currentPage - 1) * pageSize + scope.$index + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="avater" label="证件照" width="150">
                    <template slot-scope="scope">
                        <el-image style="width: 100px; height: 100px" :src="scope.row.avater"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="用户姓名" width="150"></el-table-column>
                <el-table-column prop="birthday" label="生日" width="230" align="center"></el-table-column>
                <el-table-column prop="sex.name" label="性别" width="120" align="center"></el-table-column>
                <el-table-column prop="idCard" label="身份证号" width="280" align="center"></el-table-column>
                <el-table-column prop="ethnic.name" label="民族" width="120" align="center"></el-table-column>
                <el-table-column prop="degree.name" label="学历" width="120" align="center"></el-table-column>
                <el-table-column prop="graduate" label="毕业院校" width="280" align="center"></el-table-column>
                <el-table-column prop="identity.name" label="身份背景" width="120" align="center"></el-table-column>
                <el-table-column prop="domicile.name" label="户籍地" width="120" align="center"></el-table-column>
                <el-table-column prop="address" label="家庭住址" width="380" align="center"></el-table-column>
                <el-table-column prop="organization.name" label="组织" width="200" align="center"></el-table-column>
                <el-table-column prop="job.name" label="岗位" width="200" align="center"></el-table-column>
                <el-table-column prop="account" label="账号(手机号)" width="200" align="center"></el-table-column>
                <el-table-column prop="email" label="电子邮件" width="230" align="center"></el-table-column>
                <el-table-column prop="vx" label="微信" width="230" align="center"></el-table-column>
                <el-table-column prop="contact" label="联系方式" width="250" align="center"></el-table-column>
                <el-table-column prop="createTime" label="登记日期" width="230" align="center"></el-table-column>
                <el-table-column prop="modifiedTime" label="修改日期" width="230" align="center"></el-table-column>
                <el-table-column prop="outTime" label="离职日期" width="230" align="center"></el-table-column>
                <el-table-column fixed="right" label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button size="small" icon="el-icon-key" type="primary" @click="rePassword(scope.row.id)" v-if="permissions.resetPassword">
                            重 置
                        </el-button>
                        <el-button size="small" icon="el-icon-position" type="danger" @click="logOut(scope.row.id)" v-if="permissions.setLogout">
                            登 出
                        </el-button>
                        <br v-if="permissions.assignUser">
                        <br v-if="permissions.assignUser">
                        <el-button size="small" icon="el-icon-thumb" type="primary" @click="assignUser(scope.row.id)" v-if="permissions.assignUser">
                            指 派
                        </el-button>
                        <el-button size="small" icon="el-icon-thumb" type="danger" @click="cancleThisAssignUser(scope.row.id)" v-if="permissions.assignUser">
                            消 指
                        </el-button>
                        <br v-if="permissions.setRole">
                        <br v-if="permissions.setRole">
                        <el-button size="small" icon="el-icon-eleme" type="primary" @click="swapRole(scope.row.id)" v-if="permissions.setRole">
                            增 角
                        </el-button>
                        <el-button size="small" icon="el-icon-eleme" type="danger" @click="deleteRole(scope.row.id)" v-if="permissions.setRole">
                            消 角
                        </el-button>
                        <br v-if="permissions.setSalary || permissions.setArrange">
                        <br v-if="permissions.setSalary || permissions.setArrange">
                        <el-button size="small" icon="el-icon-money" type="success" @click="sendSalary(scope.row.id)" v-if="permissions.setSalary">
                            工 资
                        </el-button>
                        <el-button size="small" icon="el-icon-date" type="primary" @click="arrangement(scope.row.id)" v-if="permissions.setArrange">
                            安 排
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    align="left"
                    @current-change="changePage"
                    background
                    layout="total, prev, pager, next"
                    :page-size="pageSize"
                    :current-page="currentPage"
                    :total="total">
            </el-pagination>
        </div>
        <!--  注册用户  -->
        <el-dialog title="注册用户" :visible.sync="editFormVisibleForRegisterUser" :show-close="isClose"
                   :close-on-click-modal="isClose" center>
            <el-divider><i class="el-icon-setting"></i></el-divider>
            <el-form :model="registerUserForm" :rules="registerUserRules" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="account" label="">
                    <el-input style="font-size: 16px;" placeholder="请填写账号" v-model="registerUserForm.account">
                        <template slot="prepend"><i class="el-icon-edit"></i></template>
                    </el-input>
                </el-form-item>
                <div style="height: 20px"></div>
                <el-form-item prop="email" label="">
                    <el-input style="font-size: 16px;" placeholder="请填写邮箱" v-model="registerUserForm.email">
                        <template slot="prepend"><i class="el-icon-eleme"></i></template>
                    </el-input>
                </el-form-item>
                <div style="height: 20px"></div>
                <el-form-item prop="roleId" label="">
                    <el-select v-model="registerUserForm.roleId" multiple placeholder="请选择角色">
                        <el-option
                                v-for="item in roleOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <div style="height: 20px"></div>
                <el-select v-model="organizationValue" placeholder="请选择组织" style="margin-right: 20px"
                           @change="handleOrganizationChange">
                    <el-option-group
                            v-for="group in organizationOptions"
                            :key="group.label"
                            :label="group.label">
                        <el-option
                                v-for="item in group.options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-option-group>
                </el-select>
                <div style="height: 20px"></div>
                <el-select v-model="postValue" placeholder="请选择" style="margin-right: 20px"
                           v-if="organizationValue>0">
                    <el-option
                            v-for="item in postOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancleRegister()">取 消</el-button>
                <el-button type="primary" @click="handleRegister()">确 定</el-button>
            </div>
        </el-dialog>
        <!--  指派用户  -->
        <el-dialog title="指派用户" :visible.sync="editFormVisibleForAssign" :show-close="isClose"
                   :close-on-click-modal="isClose" center>
            <el-divider><i class="el-icon-setting"></i></el-divider>
            <el-select v-model="adminValue" placeholder="请选择组织" style="margin-right: 20px">
                <el-option-group
                        v-for="group in adminOptions"
                        :key="group.label"
                        :label="group.label">
                    <el-option
                            v-for="item in group.options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-option-group>
            </el-select>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancleAssign()">取 消</el-button>
                <el-button type="primary" @click="handleAssign()">确 定</el-button>
            </div>
        </el-dialog>
        <!--  取消指派  -->
        <el-dialog title="取消指派" :visible.sync="editFormVisibleForCancleAssign" :show-close="isClose"
                   :close-on-click-modal="isClose" center>
            <el-divider><i class="el-icon-setting"></i></el-divider>
            <el-select v-model="cancleAdminValue" placeholder="请选择组织" style="margin-right: 20px">
                <el-option
                        v-for="item in cancleAdminOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancleThisAssign()">取 消</el-button>
                <el-button type="primary" @click="handleThisAssign()">确 定</el-button>
            </div>
        </el-dialog>
        <!--  添加角色  -->
        <el-dialog title="添加角色" :visible.sync="editFormVisibleForSwapRole" :show-close="isClose"
                   :close-on-click-modal="isClose" center>
            <el-divider><i class="el-icon-setting"></i></el-divider>
            <el-select v-model="swapRoleValue" placeholder="请选择角色" multiple style="margin-right: 20px">
                <el-option
                    v-for="item in swapRoleOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancleSwapRole()">取 消</el-button>
                <el-button type="primary" @click="handleSwapRole()">确 定</el-button>
            </div>
        </el-dialog>
        <!--  消除角色  -->
        <el-dialog title="消除角色" :visible.sync="editFormVisibleForDeleteRole" :show-close="isClose"
                   :close-on-click-modal="isClose" center>
            <el-divider><i class="el-icon-setting"></i></el-divider>
            <el-select v-model="deleteRoleValue" placeholder="请选择角色" multiple style="margin-right: 20px">
                <el-option
                    v-for="item in deleteRoleOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancleDeleteRole()">取 消</el-button>
                <el-button type="primary" @click="handleDeleteRole()">确 定</el-button>
            </div>
        </el-dialog>
        <!--  新增工资  -->
        <el-dialog title="￥新增工资￥" :visible.sync="editFormVisibleForSetSalary" :show-close="isClose"
                   :close-on-click-modal="isClose" center>
            <el-divider><i class="el-icon-setting"></i></el-divider>
            <el-form :model="setSalaryForm" :rules="setSalaryFormRules" ref="setSalaryForm" label-width="0px" class="demo-ruleForm">
                <el-form-item label="" prop="basicSalary">
                    <el-input v-model="setSalaryForm.basicSalary" style="font-size: 16px;" placeholder="请输入基本工资￥">
                        <template slot="prepend"><i class="el-icon-edit"></i>基本工资</template>
                    </el-input>
                    <div style="height: 20px"></div>
                    <el-input v-model="setSalaryForm.performance" style="font-size: 16px;" placeholder="请输入绩效工资">
                        <template slot="prepend"><i class="el-icon-edit"></i>绩效工资</template>
                    </el-input>
                    <div style="height: 20px"></div>
                    <el-input v-model="setSalaryForm.insurances" style="font-size: 16px;" placeholder="请输入保险缴纳">
                        <template slot="prepend"><i class="el-icon-edit"></i>保险缴纳</template>
                    </el-input>
                    <div style="height: 20px"></div>
                    <el-input v-model="setSalaryForm.fund" style="font-size: 16px;" placeholder="请输入公积金">
                        <template slot="prepend"><i class="el-icon-edit"></i>公积金</template>
                    </el-input>
                    <div style="height: 20px"></div>
                    <el-input v-model="setSalaryForm.reward" style="font-size: 16px;" placeholder="请输入奖金">
                        <template slot="prepend"><i class="el-icon-edit"></i>奖金</template>
                    </el-input>
                    <div style="height: 20px"></div>
                    <el-input v-model="setSalaryForm.rewardReason" style="font-size: 16px;" placeholder="请输入奖金理由">
                        <template slot="prepend"><i class="el-icon-edit"></i>奖金理由</template>
                    </el-input>
                    <div style="height: 20px"></div>
                    <el-input v-model="setSalaryForm.charge" style="font-size: 16px;" placeholder="请输入扣款">
                        <template slot="prepend"><i class="el-icon-edit"></i>扣款</template>
                    </el-input>
                    <div style="height: 20px"></div>
                    <el-input v-model="setSalaryForm.rewardReason" style="font-size: 16px;" placeholder="请输入扣款理由">
                        <template slot="prepend"><i class="el-icon-edit"></i>扣款理由</template>
                    </el-input>
                    <div style="height: 20px"></div>
                    <el-input v-model="setSalaryForm.actual" style="font-size: 16px;" placeholder="请输入实发工资">
                        <template slot="prepend"><i class="el-icon-edit"></i>实发工资</template>
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancleSetSalary()">取 消</el-button>
                <el-button type="primary" @click="handleSetSalary()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import dayjs from "dayjs";

export default {
    data() {
        return {
            //是否为搜索状态
            isSearch:false,
            // 当前页
            currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
            // 一共多少页
            pageSize: 10,
            // 一共多少条
            total: 10,
            // 员工列表
            tableData: [],
            // 表格加载时的动态
            dataListLoading: false,
            //不显示弹窗的关闭按钮
            isClose: false,
            //搜索名称
            searchName: '',
            //权限区分
            permissions: {
                registerUser: false,
                assignUser:false,
                roleSet:false,
                setRole:false,
                setSalary:false,
                setArrange:false,
                resetPassword:false,
                setLogout:false
            },
            // 注册用户弹框
            editFormVisibleForRegisterUser: false,
            // 注册用户表单
            registerUserForm: {
                account: '',
                email: '',
                organizationId: 0,
                jobId: 0,
                roleId: []
            },
            // 注册用户规则
            registerUserRules: {
                account: [
                    {required: true, message: '请输入账户名', trigger: 'blur'},
                    {pattern: /^1[3-9]\d{9}$/, message: '账户名格式不正确', trigger: 'blur'}
                ],
                email: [
                    {required: true, message: '请输入邮箱', trigger: 'blur'},
                    {
                        pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
                        message: '邮箱格式不正确',
                        trigger: 'blur'
                    }
                ],
                organizationId: [
                    {required: true, message: '请选择组织', trigger: 'blur'}
                ],
                roleId: [
                    {required: true, message: '请选择角色', trigger: 'blur'}
                ],
                postId: [
                    {required: true, message: '请选择岗位', trigger: 'blur'}
                ]
            },

            // 指派管理弹窗
            editFormVisibleForAssign: false,
            //取消指派弹窗
            editFormVisibleForCancleAssign: false,
            // 组织选择控件中的选项
            organizationOptions: [],
            // 组织选择控件中选中的内容
            organizationValue: '',
            //岗位选择控件中的选型
            postOptions: [],
            // 岗位选择控件中选中的内容
            postValue: '',
            //选择的管理组织
            adminValue: '',
            //可选择的管理组织
            adminOptions: [],
            //管理的组织id
            adminArray:[],
            //选择的取消指派的组织
            cancleAdminValue: '',
            //可选择的取消指派组织
            cancleAdminOptions: [],
            //指派人员id
            assignId: 0,
            //取消指派中的人员id
            cancleAssignId: 0,


            //添加角色弹框
            editFormVisibleForSwapRole:false,
            //删除角色弹框
            editFormVisibleForDeleteRole:false,
            // 角色选择控件中的选项
            roleOptions: [],
            //新的角色
            swapRoleValue:[],
            //新增角色时的用户id
            swapUserId:'',
            //可选择的角色
            swapRoleOptions:'',
            //选中的删除角色数据
            deleteRoleValue:[],
            //删除角色选项
            deleteRoleOptions:[],
            //删除角色的用户id
            deleteRoleUserId:0,

            //新增工资记录
            editFormVisibleForSetSalary:false,
            //新增工资员工id
            setSalaryUserId:0,
            //新增工资表单
            setSalaryForm:{
                actual:0,
                basicSalary: 0,
                charge: 0,
                chargeReason: "",
                fund: 0,
                id: 0,
                insurances: 0,
                performance: 0,
                reward: 0,
                rewardReason: "",
                userId: 0
            },
            //新增工资表单的验证
            setSalaryFormRules:{
                actual: [
                    {required: true, message: '请输入实际工资', trigger: 'blur'},
                    {pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}
                ],
                basicSalary: [
                    {required: true, message: '请输入基本工资', trigger: 'blur'},
                    {pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}
                ],
                charge: [
                    {pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}
                ],
                reward: [
                    {pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}
                ],
                chargeReason: [
                    {pattern: /^.{1,100}$/, message: '最多100个字符', trigger: 'blur'}
                ],
                rewardReason: [
                    { pattern: /^.{1,100}$/, message: '最多100个字符', trigger: 'blur'}
                ],
                insurances: [
                    {pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}
                ],
                fund: [
                    {pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}
                ]
            }
        }
    },
    mounted() {
        let that = this;
        this.isSearch = false;
        that.getDataList();
        that.getRoleList();
        that.getOrganizationList();
    },
    methods: {
        // 时间格式化
        dayjs,
        //安排工作
        arrangement(id){
            this.$router.push({
                path: '/arrangement',
                query: {
                    id: id
                }
            })
        },
        //单个用户信息
        userInfo(e,message) {
            if (e.avater === null || e.avater === '') {
                if (e.sex === null) {
                    e.avater = require('@/assets/avater-default.jpg');
                } else {
                    if (e.sex.name === '男') {
                        e.avater = require('@/assets/avater_man.png');
                    } else if (e.sex.name === '女') {
                        e.avater = require('@/assets/avater_woman.png');
                    } else {
                        e.avater = require('@/assets/avater-default.jpg');
                    }
                }
            }
            if (e.name === null || e.name === '') {
                e.name = message;
            }
            if (e.birthday === null) {
                e.birthday = message;
            } else {
                e.birthday = dayjs(e.birthday).format('YYYY-MM-DD HH:mm:ss');
            }
            if (e.sex === null || e.sex === '') {
                e.sex = {name: message};
            }
            if (e.idCard === null || e.idCard === '') {
                e.idCard = message;
            }
            if (e.ethnic === null || e.ethnic === '') {
                e.ethnic = {name: message};
            }
            if (e.degree === null || e.degree === '') {
                e.degree = {name: message};
            }
            if (e.graduate === null || e.graduate === '') {
                e.graduate = message;
            }
            if (e.identity === null || e.identity === '') {
                e.identity = {name: message};
            }
            if (e.domicile === null || e.domicile === '') {
                e.domicile = {name: message};
            }
            if (e.address === null || e.address === '') {
                e.address = message;
            }
            if (e.organization === null || e.organization === '') {
                e.organization = {name: message};
            }
            if (e.job === null || e.job === '') {
                e.job = {name: message};
            }
            if (e.account === null || e.account === '') {
                e.account = message;
            }
            if (e.email === null || e.email === '') {
                e.email = message;
            }
            if (e.vx === null || e.vx === '') {
                e.vx = message;
            }
            if (e.contact === null || e.contact === '') {
                e.contact = message;
            }
            if (e.createTime === null) {
                e.createTime = message;
            } else {
                e.createTime = dayjs(e.createTime).format('YYYY-MM-DD HH:mm:ss');
            }
            if (e.modifiedTime === null) {
                e.modifiedTime = message;
            } else {
                e.modifiedTime = dayjs(e.modifiedTime).format('YYYY-MM-DD HH:mm:ss');
            }
            if (e.outTime === null) {
                e.outTime = message;
            } else {
                e.outTime = dayjs(e.outTime).format('YYYY-MM-DD HH:mm:ss');
            }
        },
        // 注册用户弹窗
        registerUser() {
            let that = this
            that.editFormVisibleForRegisterUser = true
        },
        // 确认注册用户
        handleRegister() {
            let that = this;
            that.registerUserForm.organizationId = that.organizationValue;
            that.registerUserForm.jobId = that.postValue;
            that.$http('/user/register', 'POST', that.registerUserForm, (resp) => {
                that.$message.success(resp.data.msg);
                that.getDataList();
                that.cancleRegister();
            })
        },
        // 取消注册注册用户
        cancleRegister() {
            let that = this;
            that.registerUserForm.account = '';
            that.registerUserForm.email = '';
            that.registerUserForm.roleId = [];
            that.registerUserForm.organizationId = 0;
            that.organizationValue = '';
            that.postValue = '';
            that.editFormVisibleForRegisterUser = false;
        },
        // 加载表格数据
        getDataList() {
            this.dataListLoading = true;
            let permissionArray = this.parseStringToArray(localStorage.getItem('permissions'), ',');
            //注册用户权限
            if (permissionArray.includes('user:register')) {
                this.permissions.registerUser = true;
            }
            //指派操作权限
            if (permissionArray.includes('admin:assign')) {
                this.permissions.assignUser = true;
            }
            //角色操作权限
            if (permissionArray.includes('role:set')) {
                this.permissions.setRole = true;
            }
            //工资操作权限
            if(permissionArray.includes('document:setSalary')){
                this.permissions.setSalary = true;
            }
            //工作操作权限
            if(permissionArray.includes('work:setArrange')){
                this.permissions.setArrange = true;
            }
            //重置密码权限
            if(permissionArray.includes('user:resetPassword')){
                this.permissions.resetPassword = true;
            }
            //登出用户权限
            if(permissionArray.includes('user:setLogout')){
                this.permissions.setLogout = true;
            }
            let that = this
            let page = that.$router.currentRoute.query.page;
            if (!page) {
                page = 1;
            }
            let roleArray = that.parseStringToArray(localStorage.getItem('roles'), ',')
            if (roleArray.includes('系统管理员')) {
                that.getAdminList();
            } else {
                that.getNoAdminList();
            }
        },
        //加载管理员的表格数据
        getAdminList(){
            let that = this;
            that.$http('/user/getAllUserList', 'POST', {
                pageNum: that.currentPage
            }, (resp) => {
                if(resp.data.code === 20000){
                    let data = resp.data.data;
                    that.tableData = data.list;
                    if(data.list.length <= 0){
                        this.currentPage = 1;
                        this.reLoadAdminList();
                    }else{
                        let message = '暂无';
                        for (let e of that.tableData) {
                            that.userInfo(e,message);
                        }
                    }
                }
                that.currentPage = resp.data.data.currentPage;
                that.pageSize = resp.data.data.pageSize;
                that.total = resp.data.data.total;
                this.dataListLoading = false;
            });
        },
        //重新加载管理员表格数据
        reLoadAdminList(){
            let that = this;
            that.$http('/user/getAllUserList', 'POST', {
                pageNum: that.currentPage
            }, (resp) => {
                if (resp.data.code === 20000) {
                    let data = resp.data.data;
                    that.tableData = data.list;
                    if (data.list.length <= 0) {
                        this.currentPage = 1;
                    } else {
                        let message = '暂无';
                        for (let e of that.tableData) {
                            that.userInfo(e, message);
                        }
                    }
                    this.dataListLoading = false;
                }
                that.currentPage = resp.data.data.currentPage;
                that.pageSize = resp.data.data.pageSize;
                that.total = resp.data.data.total;
            })
        },
        //加载非管理员的表格数据
        getNoAdminList(){
            let that = this;
            //获取当前用户的id
            let userId = localStorage.getItem('userId');
            //创建当前管理的组织数组
            let adminArray = [];
            //获取当前用户的id所管理的组织id
            that.$http('/admin/getAdminOrganizations', 'POST', {
                id: userId
            }, (resp) => {
                let data = resp.data;
                if (data.code === 20000) {
                    let list = data.organizations;
                    if (list.length > 0) {
                        for (let i = 0; i < list.length; i++) {
                            adminArray[i] = list[i].organizationId;
                        }
                        this.adminArray = adminArray;
                    }
                    if (adminArray.length <= 0) {
                        this.dataListLoading = false;
                    } else {
                        that.$http('/user/groupOrgUserList','POST',{
                            orgIds:adminArray,
                            pageNum:this.currentPage
                        },(resp)=>{
                            if(resp.data.code === 20000){
                                let data = resp.data.data;
                                if(data.list.length <= 0){
                                    this.currentPage = 1;
                                    this.reLoadNoAdminList();
                                }
                            }
                            that.tableData = resp.data.data.list;
                            let message = '暂无';
                            for (let e of that.tableData) {
                                that.userInfo(e,message);
                            }
                            that.currentPage = resp.data.data.currentPage;
                            that.pageSize = resp.data.data.pageSize;
                            that.total = resp.data.data.total;
                            this.dataListLoading = false;
                        })
                    }
                }
            });
        },
        //重新加载非管理员的表格数据
        reLoadNoAdminList(){
            let that = this;
            that.$http('/user/groupOrgUserList','POST',{
                orgIds:this.adminArray,
                pageNum:this.currentPage
            },(resp)=>{
                if(resp.data.code === 20000) {
                    let data = resp.data.data;
                    if (data.list.length <= 0) {
                        this.currentPage = 1;
                    } else {
                        that.tableData = resp.data.data.list;
                        let message = '暂无';
                        for (let e of that.tableData) {
                            that.userInfo(e, message);
                        }
                    }
                    this.dataListLoading = false;
                }
                that.currentPage = resp.data.data.currentPage;
                that.pageSize = resp.data.data.pageSize;
                that.total = resp.data.data.total;
            })
        },
        //根据用户姓名搜索用户
        search(){
            let that = this;
            this.isSearch = true;
            if (that.searchName === '') {
                that.$message({
                    type: 'error',
                    message: '请先填写需要搜索的名称'
                });
                this.isSearch=false;
            }
            let roleArray = that.parseStringToArray(localStorage.getItem('roles'), ',')
            if (roleArray.includes('系统管理员')) {
                this.adminSearch();
            } else {
                this.noAdminSearch();
            }

        },
        //非管理员重新搜索
        reNoAdminSearch(){
            let that = this;
            this.currentPage = 1;
            that.$http('/user/searchInOrganizationids','POST',{
                searchName:that.searchName,
                orgIds:that.adminArray,
                pageNum:that.currentPage
            },(resp)=>{
                let data = resp.data;
                if(data.code===20000){
                    that.tableData = data.data.list;
                    if(data.list.length>0){
                        let message = '暂无';
                        for (let e of that.tableData) {
                            this.userInfo(e,message);
                        }
                        that.currentPage = resp.data.data.currentPage;
                        that.pageSize = resp.data.data.pageSize;
                        that.total = resp.data.data.total;
                    }else {
                        that.$message({
                            type: 'info',
                            message: '未搜索到结果，请重试！'
                        });
                    }
                }
            })
        },
        //非管理员搜素
        noAdminSearch(){
            let that = this;
            that.$http('/user/searchInOrganizationids','POST',{
                searchName:that.searchName,
                orgIds:that.adminArray,
                pageNum:that.currentPage
            },(resp)=>{
                let data = resp.data;
                if(data.code===20000){
                    that.tableData = data.data.list;
                    if(data.list.length>0){
                        let message = '暂无';
                        for (let e of that.tableData) {
                            this.userInfo(e,message);
                        }
                        that.currentPage = resp.data.data.currentPage;
                        that.pageSize = resp.data.data.pageSize;
                        that.total = resp.data.data.total;
                    }else {
                        this.reNoAdminSearch();
                    }
                }
            })
        },
        //管理员重新搜索
        reAdminSearch(){
            let that = this;
            that.currentPage = 1;
            that.$http('/user/searchByname','POST',{
                searchName:that.searchName,
                pageNum:that.currentPage
            },(resp)=>{
                let data = resp.data;
                if(data.code===20000){
                    that.tableData = data.data.list;
                    if(data.list.length>0){
                        let message = '暂无';
                        for (let e of that.tableData) {
                            this.userInfo(e,message);
                        }
                        that.currentPage = resp.data.data.currentPage;
                        that.pageSize = resp.data.data.pageSize;
                        that.total = resp.data.data.total;
                    }else {
                        that.$message({
                            type: 'info',
                            message: '未搜索到结果，请重试！'
                        });
                    }
                }
            })
        },
        //管理员搜索
        adminSearch(){
            let that = this;
            that.$http('/user/searchByname','POST',{
                searchName:that.searchName,
                pageNum:that.currentPage
            },(resp)=>{
                let data = resp.data;
                if(data.code===20000){
                    that.tableData = data.data.list;
                    if(data.list.length>0){
                        let message = '暂无';
                        for (let e of that.tableData) {
                            this.userInfo(e,message);
                        }
                        that.currentPage = resp.data.data.currentPage;
                        that.pageSize = resp.data.data.pageSize;
                        that.total = resp.data.data.total;
                    }else {
                        this.reAdminSearch();
                    }
                }
            })
        },
        // 删除角色
        handleDeleteRole(){
            this.$confirm('是否确定删除该用户的所选角色？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http('/role/deleteUserRole','DELETE',{
                    id:this.deleteRoleUserId,
                    roleIds:this.deleteRoleValue
                },(resp)=>{
                    let data = resp.data;
                    if(data.code===20000){
                        this.$message({
                            type: 'success',
                            message: data.msg
                        });
                        this.editFormVisibleForDeleteRole = false;
                        this.cancleDeleteRole()
                    }else{
                        this.$message({
                            type: 'error',
                            message: data.msg
                        });
                    }
                })
            }).catch(() => {

            })
        },
        //取消删除角色
        cancleDeleteRole(){
            this.deleteRoleUserId = 0;
            this.deleteRoleOptions=[];
            this.deleteRoleValue=[];
            this.editFormVisibleForDeleteRole = false;
        },
        //删除用户id下的角色
        deleteRole(id){
            this.deleteRoleUserId=id;
            this.editFormVisibleForDeleteRole = true;
            this.$http('/role/roleList','POST',{
                id:id
            },(resp)=>{
                let data = resp.data;
                if(data.code===20000){
                    this.deleteRoleOptions = data.data.map(item=>{
                        return {
                            value:item.id,
                            label:item.name
                        }
                    })
                }else{
                    this.$message({
                        type: 'error',
                        message: data.msg
                    });
                }
            })
        },
        // 添加角色
        handleSwapRole(){
            this.$confirm('确定为该用户添加新的角色吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http('/role/setRoles','POST',{
                    id:this.swapUserId,
                    roleIds:this.swapRoleValue
                },(resp)=>{
                    let data = resp.data;
                    if(data.code===20000){
                        this.$message({
                            type: 'success',
                            message: data.msg
                        });
                        this.editFormVisibleForSwapRole = false;
                        this.cancleSwapRole();
                    }else{
                        this.$message({
                            type: 'error',
                            message: data.msg
                        });
                    }
                })
            }).catch(()=>{

            })
        },
        // 取消添加角色
        cancleSwapRole(){
            this.swapRoleValue = '';
            this.swapRoleOptions = [];
            this.editFormVisibleForSwapRole = false;
        },
        // 添加用户id下的角色
        swapRole(id){
            this.editFormVisibleForSwapRole = true;
            this.swapUserId= id;
            this.$http('/role/otherRoles','POST',{
                id:id
            },(resp)=>{
                let data = resp.data;
                if(data.code === 20000){
                    let roles = data.data;
                    for(let i = 0;i<roles.length;i++){
                        this.swapRoleOptions.push({
                            value:roles[i].id,
                            label:roles[i].name
                        })
                    }
                }
            })
        },
        // 获取角色列表
        getRoleList() {
            let that = this;
            let roleArray = that.parseStringToArray(localStorage.getItem('roles'), ',')
            if (roleArray.includes('系统管理员')) {
                that.$http('/role/list', 'GET', null, (resp) => {
                    let data = resp.data.roles;
                    for (let e of data) {
                        that.roleOptions.push({
                            value: e.id,
                            label: e.name
                        });
                    }
                });
            }else {
                that.$http('/role/list', 'GET', null, (resp) => {
                    let data = resp.data.roles;
                    for (let e of data) {
                        if(e.name!=='系统管理员'){
                            that.roleOptions.push({
                                value: e.id,
                                label: e.name
                            });
                        }

                    }
                });
            }
        },
        //创建一条工资记录
        sendSalary(id){
            let that = this;
            that.setSalaryUserId = id;
            that.editFormVisibleForSetSalary = true;
        },
        //处理新增一条工资记录
        handleSetSalary(){
            let that = this;
            that.$refs['setSalaryForm'].validate((valid) => {
                if (valid) {
                    that.$confirm('是否确认新增该记录?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        that.setSalaryForm.userId = that.setSalaryUserId;
                        that.$http('/document/setSalary','POST',that.setSalaryForm,(resp)=>{
                            let data = resp.data;
                            if(data.code===20000){
                                that.$message({
                                    message: '已为该用户创建了一条工资记录',
                                    type: 'success'
                                });
                                that.editFormVisibleForSetSalary = false;
                                that.cancleSetSalary();
                            }
                        })
                    }).catch(()=>{
                    })
                }
            });
        },
        // 取消新增工资记录
        cancleSetSalary(){
            let that = this;
            that.setSalaryForm = {
                actual: 0,
                basicSalary: 0,
                charge: 0,
                chargeReason: "",
                fund: 0,
                id: 0,
                insurances: 0,
                performance: 0,
                reward: 0,
                rewardReason: "",
                userId: 0
            };
            that.editFormVisibleForSetSalary = false;
        },
        // 取消指派
        handleThisAssign() {
            this.$confirm('确定取消该用户的指派吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http('/admin/cancleAssign', 'POST', {
                    id: this.cancleAssignId,
                    organizationId: this.cancleAdminValue
                }, (resp) => {
                    let data = resp.data;
                    if (data.code === 20000) {
                        this.$message({
                            message: data.msg,
                            type: 'success'
                        });
                        this.cancleThisAssign();
                    }else {
                        this.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        // 取消取消指派
        cancleThisAssignUser(id) {
            this.editFormVisibleForCancleAssign = true;
            this.cancleAssignId = id;
            this.$http('/admin/getAdminOrganizations', 'POST', {
                id: id
            }, (resp) => {
                let data = resp.data;
                if (data.code === 20000) {
                    let list = data.organizations;
                    if (list.length > 0) {
                        for (let i = 0; i < list.length; i++) {
                            let item = list[i];
                            this.cancleAdminOptions.push({
                                value: item.organizationId,
                                label: item.organization.name
                            })
                        }
                    }
                } else {
                    this.$message({
                        message: resp.data.msg,
                        type: 'error'
                    });
                }
            })
        },
        // 取消取消指派
        cancleThisAssign() {
            this.cancleAssignId = 0;
            this.cancleAdminValue = '';
            this.cancleAdminOptions = [];
            this.editFormVisibleForCancleAssign = false;
        },
        // 指派用户
        assignUser(id) {
            this.assignId = id;
            this.editFormVisibleForAssign = true;
            let that = this;
            that.$http('/admin/groupOrganization', 'POST', {
                id: id
            }, (resp) => {
                if (resp.data.code === 20000) {
                    let data = resp.data.data;
                    if (data.length > 0) {
                        for (let i = 0; i < data.length; i++) {
                            let children = [];
                            if (data[i].options.length > 0) {
                                for (let j = 0; j < data[i].options.length; j++) {
                                    children.push({
                                        value: data[i].options[j].id,
                                        label: data[i].options[j].name
                                    })
                                }
                            }
                            that.adminOptions.push({
                                label: data[i].label + "直属部门",
                                options: children
                            })
                        }
                    }
                }
            })
        },
        // 取消指派
        cancleAssign() {
            this.adminValue = '';
            this.adminOptions = [];
            this.editFormVisibleForAssign = false;
        },
        // 指派
        handleAssign() {
            let that = this;
            let permissions = this.parseStringToArray(localStorage.getItem('permissions'), ',')
            that.$confirm('确定选择该组织指派为该用户吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                that.$http('/admin/assign', 'POST', {
                    id: that.assignId,
                    organizationId: that.adminValue
                }, (resp) => {
                    let data = resp.data;
                    if (data.code === 20000) {
                        that.$message({
                            type: 'success',
                            message: '指派成功!'
                        });
                        that.editFormVisibleForAssign = false;
                        this.cancleAssign();
                    }
                })
            }).catch(() => {

            })
        },
        //重置密码
        rePassword(id) {
            let that = this;
            that.$confirm('确认重置该用户的密码?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                that.$http("/user/resetPassword", 'POST', {
                    id: id
                }, (resp) => {
                    let data = resp.data;
                    if (data.code === 20000) {
                        that.$message({
                            message: '重置成功',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                })
            }).catch(() => {

            })
        },
        //强制登出用户
        logOut(id) {
            let that = this;
            that.$confirm('确定强制退出该用户吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                that.$http("/user/setLogout", 'POST', {
                    id: id
                }, (resp) => {
                    if (resp.data.code === 20000) {
                        that.$message({
                            message: '已将该用户强制退出',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            message: resp.data.msg,
                            type: 'error'
                        });
                    }
                })
            }).catch(() => {
            })
        },
        // 获取组织列表
        handleOrganizationChange() {
            this.postOptions = [];
            this.postValue = '';
            this.loadPostData();
        },
        //加载岗位数据
        loadPostData() {
            let that = this;
            that.$http('/post/getAllPost', 'POST', {
                id: that.organizationValue
            }, (resp) => {
                if (resp.data.code === 20000) {
                    let data = resp.data.data;
                    if (data.length > 0) {
                        for (let i = 0; i < data.length; i++) {
                            that.postOptions.push({
                                value: data[i].id,
                                label: data[i].name
                            })
                        }
                    }
                } else {
                    that.$message.error('获取岗位列表失败');
                }
            })
        },
        // 获取组织列表
        getOrganizationList() {
            let that = this;
            that.$http('/organization/struct', 'POST', null, (resp) => {
                let data = resp.data.data;
                if (data === null) {
                    that.$message.error('请先搭建组织架构');
                    that.organizationOptions = null;
                } else {
                    let that = this;
                    that.$http('/organization/groupOrganization', 'POST', null, (resp) => {
                        if (resp.data.code === 20000) {
                            let data = resp.data.data;
                            if (data.length > 0) {
                                for (let i = 0; i < data.length; i++) {
                                    let children = [];
                                    if (data[i].options.length > 0) {
                                        for (let j = 0; j < data[i].options.length; j++) {
                                            children.push({
                                                value: data[i].options[j].id,
                                                label: data[i].options[j].name
                                            })
                                        }
                                    }
                                    that.organizationOptions.push({
                                        label: data[i].label + "直属",
                                        options: children
                                    })
                                }
                            }
                        }
                    })
                }
            })
        },
        // 切换分页
        changePage(page) {
            this.currentPage = page;
            if(this.isSearch){
                this.search();
            }else{
                this.getDataList();
            }
        },
        //替换树节点
        replaceEmptyChildrenWithNull(obj) {
            if (Array.isArray(obj)) {
                for (const item of obj) {
                    this.replaceEmptyChildrenWithNull(item);
                }
            } else if (typeof obj === 'object' && obj !== null) {
                for (const key in obj) {
                    if (key === 'children' && Array.isArray(obj[key]) && obj[key].length === 0) {
                        obj[key] = null;
                    } else {
                        this.replaceEmptyChildrenWithNull(obj[key]);
                    }
                }
            }
        }
    }
}
</script>
<style lang="less">
.table {
  margin-top: 20px;
}
</style>